package com.zh.work.service.platform.channel.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zh.work.dao.platform.channel.ChannelQrcodeMapper;
import com.zh.work.entity.channel.ChannelQrCodeEntity;
import com.zh.work.entity.channel.QrCodeRateEntity;
import com.zh.work.service.platform.channel.QrcodeChannelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 二维码渠道设置业务逻辑实现层
 * @Author: Li.HuiNeng
 * @Create in: 2019/09/04
 * @Version: 1.0.1
 */
@Service
public class QrcodeChannelServiceImpl extends ServiceImpl<ChannelQrcodeMapper, ChannelQrCodeEntity> implements QrcodeChannelService {
    private static final Logger log = LoggerFactory.getLogger(QrcodeChannelServiceImpl.class);

    @Autowired
    private ChannelQrcodeMapper mapper;


    @Override
    public int putChannelQrCode(ChannelQrCodeEntity codeEntity) throws Exception {
        // 添加机构外键信息
        if (mapper.addQrCodeChannelBranch(codeEntity.getBranchNo(), codeEntity.getChannelCode()) <= 0) return -1;
        // 解析费率信息
        List<QrCodeRateEntity> list = JSON.parseArray(codeEntity.getQrCodeRateJson(), QrCodeRateEntity.class);
        if (list != null) {
            if (list.size() > 0) {
                list.forEach(f -> f.setChannelCode(codeEntity.getChannelCode()));
                if (list.size() <= 0) return -2;
                // 添加费率外键
                if (mapper.addQrCodeChannelRate(list) <= 0) return -3;
            } else log.info("机构费率成本为空");
        }
        // 添加二维码渠道信息
        if (!codeEntity.insert()) return -4;
        return 1;
    }


    @Transactional
    @Override
    public int modChannelQrCode(ChannelQrCodeEntity codeEntity) throws Exception {
        // 删除原先指定渠道成本费率信息
        if (mapper.delChannelRateByChannelCode(codeEntity.getChannelCode()) <= 0) return -1;
        // 添加新的费率信息
        List<QrCodeRateEntity> list = JSON.parseArray(codeEntity.getQrCodeRateJson(), QrCodeRateEntity.class);
        if (list != null) {
            if (list.size() > 0) {
                list.forEach(f -> f.setChannelCode(codeEntity.getChannelCode()));
                if (list.size() <= 0) return -2;
                if (mapper.addQrCodeChannelRate(list) <= 0) return -3;
            } else log.info("机构费率成本为空");
        }
        // 添加二维码渠道信息
        if (!codeEntity.updateById()) return -4;
        return 1;
    }

    @Override
    public int putAuditChannelQrCode(Integer status, Integer id) {
        return this.mapper.putAuditChannelQrCode(status, id);
    }

    @Override
    public List<ChannelQrCodeEntity> getChannelQrCode() {
        return this.mapper.selectList(new LambdaQueryWrapper<ChannelQrCodeEntity>()
                .select(ChannelQrCodeEntity::getChannelName, ChannelQrCodeEntity::getChannelCode)
                .eq(ChannelQrCodeEntity::getStatus, 1)
        );
    }
}
